package com.hddf.project.auth.service;

import org.hibernate.Session;

import com.hddf.project.auth.dao.UserDao;
import com.hddf.project.auth.dao_impl.UserDaoImpl;
import com.hddf.project.auth.pojos.User;

import org.kommii.mysql_conn_pool.SessionProvider;
import org.kommii.common_utils.UUID_Sepuence;
import org.kommii.encryption.MD5Handler;

public class RegisterService {
	
	private static RegisterService reg_service = null;
	private static UserDao user_dao = null;
	
	static {
		if(reg_service==null){
			reg_service = new RegisterService();
		}
		if(user_dao==null){
			user_dao = new UserDaoImpl();
		}
	}
	
	public static RegisterService getInstance(){
		return reg_service;
	}
	
	public boolean addUser(User u){
		Session s = null;
		try{
			String password = u.getPassword();
			String passwd = MD5Handler.encrypt(password);
			u.setPassword(passwd);
			u.setUser_no("");
			s = SessionProvider.getSession();
			boolean rs = user_dao.save(u, s);
			u.setUser_no(UUID_Sepuence.getUUIDSequ(12)+u.getId());
			SessionProvider.commitSession(s);
			return rs; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean updateUser(User u){
		Session s = null;
		try{
			s = SessionProvider.getSession(); 
			boolean rs = user_dao.update(u, s);
			SessionProvider.commitSession(s);
			return rs;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public String updateUserPassword(String username ,String new_pw,String old_pw){
		Session s = null;
		String password = "";
		String msg = "0";
		try{
			s = SessionProvider.getSession(); 
			User u = user_dao.getUserByName(username, s);
			if(null!=u){
				password = u.getPassword();
			}else{
				msg = username+ " 用户不存在, 请核对用户名";
				return msg;
			}
			
			String temp = MD5Handler.encrypt(old_pw); 
			if(password.equals(temp)){
				String new_password = MD5Handler.encrypt(new_pw);
				u.setPassword(new_password);
				boolean rs = user_dao.update(u, s);
				if(rs){
					msg = "1";
				}else{
					msg = "0";
				}
			}else{
				msg = "原密码不正确";
			}
			SessionProvider.commitSession(s);
			return msg;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return "0";
		}
	}
	
	public boolean deleteUser(User u){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			int user_id = u.getId();
			boolean rs = user_dao.delete(user_id, s);
			SessionProvider.commitSession(s);
			return rs;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean checkUserName(String username){
		Session s = null;
		boolean rs = false;
		try{
			s = SessionProvider.getSession();
			User u = user_dao.getUserByName(username, s);
			if(null!=u){
				rs = true;
			}
			SessionProvider.commitSession(s);
			return rs;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public User getUserByName(String username){
		Session s = null;
		boolean rs = false;
		try{
			s = SessionProvider.getSession();
			User u = user_dao.getUserByName(username, s);
			SessionProvider.commitSession(s);
			return u;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean setUserOff(int id){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = user_dao.setUserOff(id, s);
			SessionProvider.commitSession(s);
			return rs;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean setUserOn(int id){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = user_dao.setUserOn(id, s);
			SessionProvider.commitSession(s);
			return rs;
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	

}
